<template>
  <div>
    <div class="flex">
      <div>
        <el-form :inline="true" :model="where" label-position="top">
          <el-form-item>
            <el-input v-model="whereType.value" clearable>
              <template #prepend>
                <el-select v-model="whereType.key" style="width:120px">
                  <el-option :label="plugin.lang('order.code')" value="code"></el-option>
                  <el-option :label="plugin.lang('order.client_name')" value="client_name"></el-option>
                </el-select>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <div style="width: 300px"><Daterange @date="date => where.time = date" /></div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="model.page(1)" plain>
              <template #icon>
                <i class="iconfont min icon-search-max" />
              </template>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button type="primary" @click="excelRef.open()">{{ lang.t('common.excel') }}
          <template #icon>
            <i class="iconfont mini icon-download-max" />
          </template>
        </el-button>
      </div>
    </div>
    <Table @info="data => info.open(data)" @print="onPrint" @printGift="onPrintGift" @code="onScanBarCode" />
    <div class="page">
      <el-pagination
        @current-change="model.page"
        :current-page="where.page"
        :page-size="where.page_size"
        layout="total, prev, pager, next, jumper"
        :total="where.page_total"
        background>
      </el-pagination>
    </div>
    <KeyCode @ok="onScanBarCode" :window="true" />
    <Info ref="info" />
    <Excel ref="excelRef" :select="excelSelect" @load="excelLoad">
      <el-alert :title="plugin.lang('alert.excel')" type="warning" :closable="false" style="margin-bottom: 20px" />
    </Excel>
  </div>
</template>
<script>
import { ref } from 'vue'
import app from '@/app'
import math from '@/app/math'
import model from '../../model'
import Print from '../../common/print'

import Table from './table'
import Info from './info'
import Daterange from '@/components/daterange'
import Excel from '@/components/excel'
import KeyCode from '@/components/keyCode'

export default {
  components: {
    Table,
    Info,
    Daterange,
    Excel,
    KeyCode
  },
  setup() {
    // 插件
    const plugin = app.plugin.index

    // 条件
    const where = model.data

    // 查询方式
    const whereType = model.where

    // 详情
    const info = ref(null)

    // 条码查询
    const onScanBarCode = code => {
      whereType.value.key = 'code'
      whereType.value.value = code
      model.page(1)
    }

    // 打印
    const onPrint = data => {
      Print.order(data)
    }

    const onPrintGift = data => {
      Print.gift(data)
    }

    // 表格
    const excelRef = ref(null)

    // 表格可导出项
    const excelSelect = {
      account_name: {
        label: plugin.lang('order.account_name'),
        render: data => data.account_name
      },
      code: {
        label: plugin.lang('order.code'),
        render: data => data.code
      },
      client_name: {
        label: plugin.lang('order.client_name'),
        render: data => data.client_name
      },
      client_tax: {
        label: plugin.lang('order.client_tax'),
        render: data => data.client_data.tax ?? ''
      },
      client_address: {
        label: plugin.lang('order.client_address'),
        render: data => data.client_data.address ?? ''
      },
      client_zip: {
        label: plugin.lang('order.client_zip'),
        render: data => data.client_data.zip ?? ''
      },
      client_city: {
        label: plugin.lang('order.client_city'),
        render: data => data.client_data.city ?? ''
      },
      ticket: {
        label: plugin.lang('order.ticket'),
        render: data => plugin.lang('order.ticket_text.' + data.ticket)
      },
      money_tax: {
        label: plugin.lang('order.money_tax'),
        render: data => Number(data.money_tax)
      },
      money: {
        label: plugin.lang('order.money'),
        render: data => Number(data.money)
      },
      count: {
        label: plugin.lang('order.count'),
        render: data => data.count
      },
      ticket_open: {
        label: plugin.lang('order.ticket_open'),
        render: data => data.ticket_open
      },
      discount_money: {
        label: plugin.lang('order.discount_money'),
        render: data => data.discount_money
      },
      tax_money: {
        label: plugin.lang('order.tax_money'),
        render: data => Number(data.tax_money)
      },
      req_money: {
        label: plugin.lang('order.req_money'),
        render: data => Number(data.req_money)
      },
      total: {
        label: plugin.lang('order.total'),
        render: data => Number(data.total)
      },
      cost_money: {
        label: plugin.lang('order.cost_money'),
        render: data => Number(data.cost_money)
      },
      profit_money: {
        label: plugin.lang('order.profit_money'),
        render: data => Number(data.profit_money)
      },
      remarks: {
        label: plugin.lang('order.remarks'),
        render: data => data.remarks
      },
      status_time: {
        label: plugin.lang('order.status_time'),
        render: data => app.time.date(data.status_time)
      },
      created_at: {
        label: plugin.lang('order.created_at'),
        render: data => app.time.date(data.created_at)
      }
    }

    // 表格加载方法
    const excelLoad = () => {
      let page = 1
      const tax = []
      excelRef.value.load(async() => {
        // 请求数据
        const data = await model.excel(page)
        
        // 统计订单
        data.data.forEach(item => {
          // 循环订单税
          item.tax.forEach(itemTax => {
            // 是否累加
            let index = tax.findIndex(value => value.tax == itemTax.tax && value.req == itemTax.req)
            if(index == -1) {
              index = tax.push({
                tax: itemTax.tax,
                req: itemTax.req,
                money_tax: 0,
                tax_money: 0,
                req_money: 0,
                total: 0
              }) - 1
            }
            // 累加数据
            tax[index].money_tax = math.add(itemTax.money_tax, tax[index].money_tax)
            tax[index].tax_money = math.add(itemTax.tax_money, tax[index].tax_money)
            tax[index].req_money = math.add(itemTax.req_money, tax[index].req_money)
            tax[index].total = math.add(itemTax.total, tax[index].total)
          })
        })

        // 分页 + 1
        page++
        // 导出原数据
        return [data.total, data.data]
      }, () => {
        const list = []
        let money_tax = 0, tax_money = 0, req_money = 0, total = 0
        // 循环所有税
        tax.forEach(item => {
          list.push(['', item.money_tax, item.tax, item.tax_money, item.req, item.req_money, item.total])
          // 累加所有税
          money_tax = math.add(item.money_tax, money_tax)
          tax_money = math.add(item.tax_money, tax_money)
          req_money = math.add(item.req_money, req_money)
          total = math.add(item.total, total)
        })
        return [
          [],
          ['', 'Base Imponible', 'IVA%', 'Importe IVA', 'REQ%', 'Importe REQ', 'Total'],
          ...list,
          ['Subtotal', money_tax, '', tax_money, '', req_money, total]
        ]
      })
    }

    // 加载列表
    model.get()

    return {
      plugin,
      lang: app.lang,
      model,
      where,
      whereType,
      info,
      onScanBarCode,
      onPrint,
      onPrintGift,
      excelRef,
      excelSelect,
      excelLoad,
    }
  }
}
</script>